home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Graphics Samples / ShapePart Browser ƒ / Window.c < prev    next >
Encoding:
Text File  |  1996-04-15  |  8.2 KB  |  370 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    Window.c
  3.  *
  4.  *    Change History:
  5.  *
  6.  *       4/93    ???        New
  7.  *       4/96    bob        Updated #includes to support changed GX Library names.
  8.  *                    Added the copyright info.
  9.  *
  10.  *
  11.  *        © Apple Computer, Inc. 1990 - 1996  All rights reserved
  12.  *
  13.  */
  14.  
  15.  
  16. #undef    MAC_HEADERS
  17.  
  18.  
  19. /*------------------*/
  20. /*    Include Files    */
  21. /*------------------*/
  22. #ifndef    MAC_HEADERS
  23.     #include <Windows.h>
  24.     #include <Dialogs.h>
  25.     #include <Menus.h>
  26.     #include <ToolUtils.h>
  27. #endif    MAC_HEADERS
  28.  
  29. #include <GXGraphics.h>
  30. #include "GraphicsLibraries.h"    /* For commonColors */
  31.  
  32. #include "ResourceIDs.h"
  33. #include "ShapeAction.h"
  34. #include "ShapeSetup.h"
  35.  
  36. #include "Window.h"
  37.  
  38.  
  39. /*----------------------*/
  40. /*    Global Declarations    */
  41. /*----------------------*/
  42. GlobalStructure    globals;
  43.  
  44.  
  45. /*------------------------------*/
  46. /*    External Declarations     */
  47. /*------------------------------*/
  48.  
  49.  
  50. /*------------------------------*/
  51. /*       Local ProtoTypes       */
  52. /*------------------------------*/
  53. void GetShapePartControls (WindowPtr pWindow, short firstItem, short lastItem, ControlHandle *ph1stControl);
  54. void TrackCheckBox (ControlHandle hControl, Point where, Boolean *pIsChecked);
  55. void SetShapePartsFromFlags (GlobalStructure *pG, ControlHandle hControl, long shapeCount);
  56. void TrackShapePartCheckBox (ControlHandle hControlHit, short whichPart, Point where);
  57.  
  58.  
  59.     void
  60. GetShapePartControls (WindowPtr pWindow, short firstItem, short lastItem, ControlHandle *ph1stControl)
  61. {
  62.     register
  63.     short            item;
  64.     register
  65.     ControlHandle    *pControl;
  66.     short            itemType;
  67.     Rect            itemRect;
  68.  
  69.     pControl = ph1stControl;
  70.     for (item = firstItem; item <= lastItem; item++)
  71.     {
  72.         GetDItem (pWindow, item, &itemType, (Handle *) pControl, &itemRect);
  73.         if (*pControl == nil)
  74.         {
  75.             DebugStr ("\pGetShapePartControls: No checkbox found");
  76.             break;
  77.         }
  78.         pControl++;
  79.     }
  80. }
  81.  
  82.  
  83.     Boolean
  84. InitializeHitTesting (WindowPtr pWindow)
  85. {
  86.     if (! CreateShapesFrame (pWindow, kRowCount, kColumnCount, &globals.boxes))
  87.         return (false);
  88.  
  89.     if (! CreateHitTestShapes (pWindow, &globals.pShapes, &globals.boxes, kShapeCount))
  90.         return (false);
  91.  
  92.     GetShapePartControls (pWindow, checkBoxFirstShapePart, checkBoxLastShapePart, &globals.hNothing);
  93.  
  94.     /* Force SetShapePartsFromFlags to initialize the shape's shape parts */
  95.     globals.partsHit     = gxAnyPart;
  96.     globals.tolerance    = 0;
  97.  
  98.     globals.showControlPoints = false;
  99.     globals.showLocalBounds   = false;
  100.  
  101.     SetShapePartsFromFlags (&globals, globals.hNothing, kShapeCount);
  102.  
  103.     return (true);
  104. }
  105.  
  106.  
  107.     void
  108. CleanupHitTesting (void)
  109. {
  110.     DisposeHitTestShapes (&globals.pShapes, &globals.boxes);
  111. }
  112.  
  113.  
  114.     void
  115. IdleHitTestWindow (WindowPtr pWindow, EventRecord *pEvent)
  116. {
  117.     Point    where;
  118.  
  119.     where = pEvent->where;
  120.     GlobalToLocal (&where);
  121.  
  122.     if (PtInRect (where, &pWindow->portRect))
  123.         UpdateShapePartInfo (&where, &globals);
  124. }
  125.  
  126.  
  127.     void
  128. TrackCheckBox (ControlHandle hControl, Point where, Boolean *pIsChecked)
  129. {
  130.     if (TrackControl (hControl, where, nil))
  131.     {
  132.         *pIsChecked = ! *pIsChecked;
  133.         SetCtlValue (hControl, *pIsChecked);
  134.     }
  135. }
  136.  
  137.  
  138. /*
  139.  *    pG->hNothing
  140.  *    pG->hBounds
  141.  *    pG->hAnything
  142.  *
  143.  *    pG->(all)Checked
  144.  *
  145.  *    pG->partsHit
  146.  */
  147. /*
  148.     Sets all the shape's shapeParts from the state of the check-boxes
  149. */
  150.     void
  151. SetShapePartsFromFlags (GlobalStructure *pG, ControlHandle hControl, long shapeCount)
  152. {
  153.     register
  154.     gxShape            *pShape;
  155.     gxShapePart        newParts;
  156.     Boolean            *pFlag;
  157.     ControlHandle    *pControl;
  158.  
  159.     if (hControl == pG->hNothing)
  160.     {
  161.         if (pG->testNothing)
  162.         {
  163.             /* Nothing checkbox just checked on; turn off all other checkboxes */
  164.             newParts = gxNoPart;
  165.  
  166.             pFlag     = &pG->testBounds;
  167.             pControl = &pG->hBounds;
  168.  
  169.             while (pFlag <= &pG->testAnything)
  170.                 SetCtlValue (*pControl++, *pFlag++ = false);
  171.         }
  172.         else
  173.         {
  174.             /* Nothing was just checked off; turn on all other checkboxes */
  175.             pFlag     = &pG->testBounds;
  176.             pControl = &pG->hBounds;
  177.  
  178.             newParts = gxAnyPart;
  179.             while (pFlag <= &pG->testAnything)
  180.                 SetCtlValue (*pControl++, *pFlag++ = true);
  181.  
  182.             SetCtlValue (pG->hNothing, pG->testNothing = false);
  183.         }
  184.     }
  185.     else if (hControl == pG->hAnything) 
  186.     {
  187.         Boolean        newState;
  188.  
  189.         /* Anything checkbox hit */
  190.         if (pG->testAnything)
  191.         {
  192.             /* Turn on all checkboxes if Anything was already checked */
  193.             newState = true;
  194.             newParts = pG->partsHit | gxAnyPart;
  195.         }
  196.         else
  197.         {
  198.             /* Turn off all checkboxes */
  199.             newState = false;
  200.             newParts = pG->partsHit & ~gxAnyPart;
  201.         }
  202.  
  203.         pFlag     = &pG->testBounds;
  204.         pControl = &pG->hBounds;
  205.  
  206.         while (pFlag <= &pG->testPattern)
  207.             SetCtlValue (*pControl++, *pFlag++ = newState);
  208.  
  209.         /* Turn off the Nothing checkbox */
  210.         SetCtlValue (pG->hNothing, pG->testNothing = false);
  211.     }
  212.     else
  213.     {
  214.         /* An individual checkbox was hit */
  215.         newParts = gxNoPart;
  216.  
  217.         if (pG->testNothing)
  218.             SetCtlValue (pG->hNothing, pG->testNothing = false);
  219.         if (pG->testBounds)            newParts |= gxBoundsPart;
  220.         if (pG->testGeometry)        newParts |= gxGeometryPart;
  221.         if (pG->testPen)            newParts |= gxPenPart;
  222.         if (pG->testCornerPoint)    newParts |= gxCornerPointPart;
  223.         if (pG->testControlPoint)    newParts |= gxControlPointPart;
  224.         if (pG->testEdge)            newParts |= gxEdgePart;
  225.         if (pG->testJoin)            newParts |= gxJoinPart;
  226.         if (pG->testStartCap)        newParts |= gxStartCapPart;
  227.         if (pG->testEndCap)            newParts |= gxEndCapPart;
  228.         if (pG->testDash)            newParts |= gxDashPart;
  229.         if (pG->testPattern)        newParts |= gxPatternPart;
  230.  
  231.         if (pG->testAnything    &&
  232.             (newParts & gxAnyPart) != gxAnyPart)
  233.             SetCtlValue (pG->hAnything, pG->testAnything = false);
  234.     }
  235.  
  236.     if (newParts == gxNoPart)
  237.         SetCtlValue (pG->hNothing, pG->testNothing = true);
  238.     else if ((newParts & gxAnyPart) == gxAnyPart  &&
  239.              (! pG->testAnything))
  240.     {
  241.         SetCtlValue (pG->hAnything, pG->testAnything = true);
  242.     }
  243.  
  244.     /* Set the new parts tested on all shapes */
  245.     for (pShape = pG->pShapes + shapeCount - 1;
  246.          pShape >= pG->pShapes;
  247.          pShape--)
  248.     {
  249.         GXSetShapeHitTest (*pShape, newParts, pG->tolerance);
  250.     }
  251.     pG->partsHit = newParts;
  252. }
  253.  
  254.  
  255.     void
  256. TrackShapePartCheckBox (ControlHandle hControlHit, short whichPart, Point where)
  257. {
  258.     Rect    bounds;
  259.  
  260.     if (hControlHit == globals.hNothing)
  261.     {    TrackCheckBox (globals.hNothing, where, &globals.testNothing);
  262.         return;
  263.     }
  264.  
  265.     if (hControlHit == globals.hBounds)
  266.     {    TrackCheckBox (globals.hBounds, where, &globals.testBounds);
  267.         return;
  268.     }
  269.  
  270.     if (hControlHit == globals.hGeometry)
  271.     {    TrackCheckBox (globals.hGeometry, where, &globals.testGeometry);
  272.         return;
  273.     }
  274.  
  275.     if (hControlHit == globals.hPen)
  276.     {    TrackCheckBox (globals.hPen, where, &globals.testPen);
  277.         return;
  278.     }
  279.  
  280.     if (hControlHit == globals.hCornerPoint)
  281.     {    TrackCheckBox (globals.hCornerPoint, where, &globals.testCornerPoint);
  282.         return;
  283.     }
  284.  
  285.     if (hControlHit == globals.hControlPoint)
  286.     {    TrackCheckBox (globals.hControlPoint, where, &globals.testControlPoint);
  287.         return;
  288.     }
  289.  
  290.     if (hControlHit == globals.hEdge)
  291.     {    TrackCheckBox (globals.hEdge, where, &globals.testEdge);
  292.         return;
  293.     }
  294.  
  295.     if (hControlHit == globals.hJoin)
  296.     {    TrackCheckBox (globals.hJoin, where, &globals.testJoin);
  297.         return;
  298.     }
  299.  
  300.     if (hControlHit == globals.hStartCap)
  301.     {    TrackCheckBox (globals.hStartCap, where, &globals.testStartCap);
  302.         return;
  303.     }
  304.  
  305.     if (hControlHit == globals.hEndCap)
  306.     {    TrackCheckBox (globals.hEndCap, where, &globals.testEndCap);
  307.         return;
  308.     }
  309.  
  310.     if (hControlHit == globals.hDash)
  311.     {    TrackCheckBox (globals.hDash, where, &globals.testDash);
  312.         return;
  313.     }
  314.  
  315.     if (hControlHit == globals.hPattern)
  316.     {    TrackCheckBox (globals.hPattern, where, &globals.testPattern);
  317.         return;
  318.     }
  319.  
  320.     TrackCheckBox (globals.hAnything, where, &globals.testAnything);
  321. }
  322.  
  323.  
  324.     void
  325. DoHitTestContent (EventRecord *pEvent, WindowPtr pWindow)
  326. {
  327.     Point            where;
  328.     ControlHandle    hControl;
  329.     short            partCode;
  330.  
  331.     where = pEvent->where;
  332.     GlobalToLocal (&where);
  333.  
  334.     partCode = FindControl (where, pWindow, &hControl);
  335.  
  336.     if (hControl != nil)
  337.     {
  338.         TrackShapePartCheckBox (hControl, partCode, where);
  339.         SetShapePartsFromFlags (&globals, hControl, kShapeCount);
  340.     }
  341. }
  342.  
  343.  
  344.     void
  345. ToggleMetricsMenuItem (short item, WindowPtr pWIndow)
  346. {
  347.     Boolean        *pState;
  348.     Str255        menuItemStr;
  349.  
  350.     switch (item)
  351.     {
  352.         case itemShowControlPoints:
  353.             pState = &globals.showControlPoints;
  354.             break;
  355.  
  356.         case itemShowLocalBounds:
  357.             pState = &globals.showLocalBounds;
  358.             break;
  359.     }
  360.  
  361.     *pState = ! *pState;
  362.     GetIndString (menuItemStr, metricsMenuStringsID, *pState + 2 * item - itemShowControlPoints);
  363.     SetItem (GetMHandle (metricsMenuRsrcID), item, menuItemStr);
  364.  
  365.     if (*pState  &&  item == itemShowControlPoints)
  366.         ShowControlPoints (globals.pShapes, kShapeCount);
  367.     else
  368.         UpdateHitTestWindow (pWIndow);
  369. }
  370.